package org.csu.laboratoryequipment.controller;

import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.CompKeyWord;
import org.csu.laboratoryequipment.entity.Result;
import org.csu.laboratoryequipment.service.HistoryService;
import org.csu.laboratoryequipment.service.ManagerService;
import org.csu.laboratoryequipment.service.ResultService;
import org.csu.laboratoryequipment.service.SearchrecordService;
import org.csu.laboratoryequipment.service.impl.SearchrecordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/")
public class ManagerController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private SearchrecordService searchrecordService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private ResultService resultService;

    @ResponseBody
    //判断用户名密码是否正确
    @GetMapping("login")
    public CommonResponse login(
            @RequestParam("username") String username,
            @RequestParam("password") String password

    ) {

        return managerService.login(username,password);
    }

    @ResponseBody
    //输出搜索量排名
    @GetMapping("getRank")
    public CommonResponse getRank(

    ) {

        return searchrecordService.getRank();
    }

    @ResponseBody
    //根据时间范围输出搜索量排名
    @GetMapping("getKeywordByTime")
    public CommonResponse getKeywordByTime(
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime

    ) throws ParseException {

        //将字符串类型数据转换成date类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime1 = dateFormat.parse(startTime);
        Date startTime2 = dateFormat.parse(endTime);
        return searchrecordService.getKeywordByTime(startTime1,startTime2);
    }

    @ResponseBody
    //根据时间输出搜索量排名
    @GetMapping("getKeyword")
    public CommonResponse getKeyword(

    ) throws ParseException {

        return searchrecordService.getKeyword();
    }

    @ResponseBody
    //根据时间范围输出搜索量排名
    @GetMapping("getHistoryByTime")
    public CommonResponse getHistoryByTime(
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime

    ) throws ParseException {

        //将字符串类型数据转换成date类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime1 = dateFormat.parse(startTime);
        Date startTime2 = dateFormat.parse(endTime);
        return historyService.getHistoryByTime(startTime1,startTime2);
    }

    @ResponseBody
    //根据时间输出搜索量排名
    @GetMapping("getHistory")
    public CommonResponse getHistory(

    ) throws ParseException {

        return historyService.getHistory();
    }

    @ResponseBody
    //根据时间输出搜索量排名
    @GetMapping("getResult")
    public CommonResponse getResult(

    ) throws ParseException {

        return resultService.getResult();
    }

    @ResponseBody
    @PostMapping("getResultByCompKey")
    public CommonResponse<List<Result>> getResultByCompKey(@RequestParam("compKey") String seedWord) throws Exception {
        List<Result> temp = new ArrayList<>();
        temp = resultService.searchByseedWord(seedWord);
        //先去result查找如果有
        if (!temp.isEmpty()) {
            return CommonResponse.createForSuccessMessage("SUCCESS", temp);
        }
        //result中没有，再用算法生成
        //result中的竞争关键词，竞争度
        List<CompKeyWord> result = new ArrayList<>();
        result = resultService.getCompKeyAndComNumBySeedWord(seedWord);
        //将结果插入result表
        resultService.addRecord(seedWord, result);
        return CommonResponse.createForSuccessMessage("SUCCESS", resultService.searchByseedWord(seedWord));
    }

}
